import { 
  Controller, 
  Get, 
  Post, 
  Put, 
  Delete, 
  Body, 
  Param, 
  Query, 
  ParseIntPipe 
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBearerAuth } from '@nestjs/swagger';
import { Roles, CurrentUser } from '@app/auth';
import { 
  CreateTagDto, 
  UpdateTagDto, 
  QueryTagsDto, 
  BatchTagActionDto,
  MergeTagsDto,
  TagStatsDto 
} from '../dto';
import { TagsService } from '../services/tags.service';

@ApiTags('标签管理')
@ApiBearerAuth()
@Controller('admin/tags')
export class TagsController {
  constructor(private readonly tagsService: TagsService) {}

  @Get()
  @ApiOperation({ summary: '获取标签列表' })
  @ApiResponse({ status: 200, description: '返回标签列表' })
  @Roles('admin', 'editor')
  async findAll(@Query() query: QueryTagsDto) {
    return await this.tagsService.findAll(query);
  }

  @Get('popular')
  @ApiOperation({ summary: '获取热门标签' })
  @ApiResponse({ status: 200, description: '返回热门标签列表' })
  @Roles('admin', 'editor')
  async getPopular(@Query() query: { limit?: number }) {
    return await this.tagsService.findPopular(query?.limit ?? 10);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取标签详情' })
  @ApiParam({ name: 'id', description: '标签ID' })
  @ApiResponse({ status: 200, description: '返回标签详情' })
  @Roles('admin', 'editor')
  async findOne(@Param('id', ParseIntPipe) id: number) {
    return await this.tagsService.findOne(id);
  }

  @Post()
  @ApiOperation({ summary: '创建标签' })
  @ApiResponse({ status: 201, description: '标签创建成功' })
  @Roles('admin', 'editor')
  async create(@Body() createTagDto: CreateTagDto, @CurrentUser() user: any) {
    return await this.tagsService.create(createTagDto);
  }

  @Put(':id')
  @ApiOperation({ summary: '更新标签' })
  @ApiParam({ name: 'id', description: '标签ID' })
  @ApiResponse({ status: 200, description: '标签更新成功' })
  @Roles('admin', 'editor')
  async update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateTagDto: UpdateTagDto,
    @CurrentUser() user: any
  ) {
    return await this.tagsService.update(id, updateTagDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除标签' })
  @ApiParam({ name: 'id', description: '标签ID' })
  @ApiResponse({ status: 200, description: '标签删除成功' })
  @Roles('admin')
  async remove(@Param('id', ParseIntPipe) id: number) {
    await this.tagsService.remove(id);
    return null;
  }

  @Post('batch')
  @ApiOperation({ summary: '批量操作标签' })
  @ApiResponse({ status: 200, description: '批量操作成功' })
  @Roles('admin', 'editor')
  async batchAction(@Body() batchActionDto: BatchTagActionDto) {
    return await this.tagsService.batchAction(batchActionDto);
  }

  @Get(':id/posts')
  @ApiOperation({ summary: '获取标签下的文章' })
  @ApiParam({ name: 'id', description: '标签ID' })
  @ApiResponse({ status: 200, description: '返回标签下的文章列表' })
  @Roles('admin', 'editor')
  async getTagPosts(@Param('id', ParseIntPipe) id: number, @Query() query: any) {
    return await this.tagsService.getTagPosts(id, query);
  }

  @Post(':id/merge')
  @ApiOperation({ summary: '合并标签' })
  @ApiParam({ name: 'id', description: '源标签ID' })
  @ApiResponse({ status: 200, description: '标签合并成功' })
  @Roles('admin')
  async mergeTags(@Param('id', ParseIntPipe) sourceId: number, @Body() body: { targetId: number }) {
    return await this.tagsService.mergeTags([sourceId], body.targetId);
  }

  @Get('stats/usage')
  @ApiOperation({ summary: '获取标签使用统计' })
  @ApiResponse({ status: 200, description: '返回标签使用统计' })
  @Roles('admin', 'editor')
  async getUsageStats(@Query() query: TagStatsDto) {
    return await this.tagsService.getUsageStats(query);
  }
}
